<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div @mouseleave="leaveIndex" @mouseenter="enterShow">
        <h2 class="all">全部商品分类</h2>
        <transition name="sort">
          <div class="sort" v-show="isShow">
            <!-- 事件委派+声明式导航+自定义属性+event -->
            <div class="all-sort-list2" @click="goSearch">
              <div
                class="item"
                v-for="(c1, index) in categoryList"
                :key="c1.id"
                :class="{ cur: currentIndex == index }"
              >
                <h3 @mouseenter="changeIndex(index)">
                  <a
                    :data-categoryName="c1.categoryName"
                    :data-category1Id="c1.categoryId"
                    >{{ c1.categoryName }}</a
                  >
                </h3>
                <div
                  class="item-list clearfix"
                  :style="{
                    display: currentIndex === index ? 'block' : 'none',
                  }"
                >
                  <div
                    class="subitem"
                    v-for="c2 in c1.categoryChild"
                    :key="c2.categoryId"
                  >
                    <dl class="fore">
                      <dt>
                        <a
                          :data-categoryName="c2.categoryName"
                          :data-category2Id="c2.categoryId"
                          >{{ c2.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                          <a
                            :data-categoryName="c3.categoryName"
                            :data-category3Id="c3.categoryId"
                            >{{ c3.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
// import store from "@/store";
import { mapState } from "vuex";
// lodash用来防抖和节流
import throttle from "lodash/throttle";

export default {
  name: "TypeNav",
  data() {
    return {
      currentIndex: -1,
      isShow: true,
    };
  },
  mounted() {
    if (this.$route.path != "/home") {
      this.isShow = false;
    }
  },
  computed: {
    ...mapState({
      categoryList: (state) => state.home.categoryList,
    }),
  },
  methods: {
    leaveIndex() {
      if (this.$route.path !== "/home") {
        this.isShow = false;
      }
      this.currentIndex = -1;
    },
    changeIndex: throttle(function (index) {
      this.currentIndex = index;
    }, 50),
    goSearch(e) {
      let target = e.target;
      let { categoryname, category1id, category2id, category3id } =
        target.dataset;
      if (categoryname) {
        let location = { name: "search" };

        let query = {};
        query.categoryName = categoryname;

        if (category1id) {
          query.category1Id = category1id;
        } else if (category2id) {
          query.category2Id = category2id;
        } else {
          query.category3Id = category3id;
        }

        if (this.$route.params) {
          location.params = this.$route.params;
        }
        location.query = query;
        this.$router.push(location);
      }
    },
    enterShow() {
      this.isShow = true;
    },
  },
};
</script>

<style lang='less'>
.cur {
  background-color: #e1251b;
}

.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      line-height: 45px;
      text-align: center;
      background-color: #e1251b;
      color: #fff;
      font-size: 16px;
      font-weight: bold;
      cursor: pointer;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 20px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }

      a:first-child {
        margin-left: 40px;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 44px;
      width: 210px;
      height: 463px;
      position: absolute;
      border-top: solid 3px #e1251b;
      background-color: #fafafa;
      padding: 15px 0;
      box-sizing: border-box;
      z-index: 99 !important;

      .all-sort-list2 {
        height: 100%;
        overflow: hidden;

        .item {
          cursor: default;
          h3 {
            height: 43px;
            line-height: 43px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            // display: none;
            position: absolute;
            width: 700px;
            min-height: 412px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            padding: 20px 15px;
            top: 5px;
            z-index: 999 !important;

            .subitem {
              float: left;
              width: 700px;

              dl {
                overflow: hidden;
                zoom: 1;
                padding: 10px 0px;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 100px;
                  height: 25px;
                  line-height: 25px;
                  font-size: 15px;
                  padding: 0px 10px 0px 0px;
                  box-sizing: border-box;
                  text-align: right;
                  font-weight: 700;
                  cursor: default;
                }

                dd {
                  float: left;
                  width: 570px;
                  font-size: 14px;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 25px;
                    line-height: 25px;
                    padding: 0 8px;
                    cursor: default;
                  }
                }
              }
            }
          }

          // &:hover {
          //   .item-list {
          //     display: block;
          //   }
          // }
        }
      }
    }

    .sort-enter {
      height: 0px;
    }
    .sort-enter-to {
      height: 463px;
    }
    .sort-enter-active {
      transition: all 0.5s linear;
    }
  }
}
</style>